package com.zjsy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zjsy.po.Magazine;
import com.zjsy.po.MainNewsModule;
import com.zjsy.po.Message;
import com.zjsy.po.FriendLink;
import com.zjsy.po.FriendLinkModule;
import com.zjsy.service.FriendLinkService;
import com.zjsy.service.MagazineService;
import com.zjsy.service.MainNewsService;

@Controller
public class FriendController {
	
	@Autowired
	private FriendLinkService friendLinkservice;
	@Autowired
	private MainNewsService mainNewsservice;
	
	@RequestMapping("/friend-list")
	public String friendList(HttpServletRequest req,HttpServletResponse resp) {
		List<FriendLink> list=friendLinkservice.findFriendLink();
		List<MainNewsModule> moudulelist=mainNewsservice.findNewsModule("4");
		req.setAttribute("list", list);
		req.setAttribute("moudulelist", moudulelist);
		return "friend-list";
	}
	
	@RequestMapping("/friend-add")
	public String friendAdd(HttpServletRequest req,HttpServletResponse resp) {
		List<MainNewsModule> moudulelist=mainNewsservice.findNewsModule("4");
		req.setAttribute("moudulelist", moudulelist);
		return "friend-add";
	}
	
	
	
	@RequestMapping("/addFriend")
	@ResponseBody
	public Message addFriend(HttpServletRequest req,HttpServletResponse resp, @RequestBody FriendLinkModule friend) {
        System.out.println(666666);
		friendLinkservice.addFriend(friend);
		return new Message("添加友情链接成功");
	}
	
	
	
	@RequestMapping("/friend-edit")
	public String friendEdit(HttpServletRequest req,HttpServletResponse resp) {
		
		String id=req.getParameter("id");
		List<MainNewsModule> moudulelist=mainNewsservice.findNewsModule("4");
		FriendLinkModule friend=friendLinkservice.findFreindLinkById(id);
        req.setAttribute("moudule", friend);
		req.setAttribute("moudulelist", moudulelist);
		return "friend-edit";
	}
	
	@RequestMapping("/updateFriend")
	@ResponseBody
	public Message updateFriend(HttpServletRequest req,HttpServletResponse resp, @RequestBody FriendLinkModule friend) {
		friendLinkservice.uopdateFreindLink(friend);
		return new Message("更新成功");
	}
	
	@RequestMapping("/deleteFriend")
	@ResponseBody
	public Message deleteFriend(HttpServletRequest req,HttpServletResponse resp) {
		
		String id=req.getParameter("id");
		friendLinkservice.deleteFriendById(id);
		
		return new Message("删除成功");
	}
	
	
	
}
